﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebsiteBanThucAn.Models;
using System.Data;
using WebsiteBanThucAn.DAL;

namespace DAL
{
    public class QuangCaoDAL
    {
        private static void GanDoiTuong(QuangCaoModel qcm, DataRow dr)
        {// Gán từng thuộc tính cho đối tượng
            qcm.QuangcaoID = int.Parse(dr[QuangCaoModel.quangcaoid].ToString());
            qcm.Tencongty = dr[QuangCaoModel.tencongty].ToString();
            qcm.Hinhquangcao = dr[QuangCaoModel.hinhquangcao].ToString();
            qcm.DiachiWS = dr[QuangCaoModel.diachiws].ToString();
            qcm.Ngaybatdau = DateTime.Parse(dr[QuangCaoModel.ngaybatdau].ToString());
            qcm.Ngayketthuc = DateTime.Parse(dr[QuangCaoModel.ngayketthuc].ToString());
        }

        public static List<QuangCaoModel> LayDSQuangCaoTatCa()
        {// Lấy danh sách tất cả món ăn
            List<QuangCaoModel> list = new List<QuangCaoModel>();
            try
            {
                string sql = string.Format("select * from {0}", QuangCaoModel.quangcao);
                DataTable dt = SqlDataProvider.DocBang(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    QuangCaoModel qcm = new QuangCaoModel();
                    GanDoiTuong(qcm, dr);
                    list.Add(qcm);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static QuangCaoModel LayThongTinQuangCaoTheoID(int id)
        {// Lấy thông tin món ăn theo ID
            QuangCaoModel qcm = new QuangCaoModel();
            try
            {
                string sql = string.Format("select * from {0} where {1}='{2}'", QuangCaoModel.quangcao, QuangCaoModel.quangcaoid, id);
                DataTable dt = SqlDataProvider.DocBang(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    GanDoiTuong(qcm, dr);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return qcm;
        }

        public static bool ThemQuangCao(QuangCaoModel qcm)
        {
            bool result = false;
            try
            {
                string sql = string.Format("insert into {0} values ", QuangCaoModel.quangcao);
                sql += string.Format("(N'{0}',N'{1}','{2}',convert(date,'{3}',101),convert(date,'{4}',101))",
                    qcm.Tencongty, qcm.Hinhquangcao, qcm.DiachiWS, qcm.Ngaybatdau, qcm.Ngayketthuc);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool XoaQuangCao(int id)
        {
            bool result = false;
            try
            {
                string sql = string.Format("delete {0} where {1}='{2}'", QuangCaoModel.quangcao, QuangCaoModel.quangcaoid, id);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool SuaQuangCao(QuangCaoModel qcm)
        {
            bool result = false;
            try
            {
                string sql = string.Format("update {0} set ", QuangCaoModel.quangcao);
                sql += string.Format("{0}=N'{1}',{2}=N'{3}',{4}='{5}',{6}=convert(date,'{7}',101),{8}=convert(date,'{9}',101)",
                    QuangCaoModel.tencongty,
                    qcm.Tencongty,
                    QuangCaoModel.hinhquangcao,
                    qcm.Hinhquangcao,
                    QuangCaoModel.diachiws,
                    qcm.DiachiWS,
                    QuangCaoModel.ngaybatdau,
                    qcm.Ngaybatdau,
                    QuangCaoModel.ngayketthuc,
                    qcm.Ngayketthuc);
                sql += string.Format(" where {0}='{1}'", QuangCaoModel.quangcaoid, qcm.QuangcaoID);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
